<?php

class DefaultController extends BackOfficeController
{
	public function getGuestAllowedActions() {
        return array(
            'login', 'forgotPassword', 'error', 'activateAccount', 'confirmResetPassword'
        );
    }
    
    public function actionIndex()
	{
		$this->render('index');
	}
    
    /**
    * Login in BackOffice
    */
    public function actionLogin(){
        Yii::import('User.models.User');
        $user = new User('login');
        $invalidStatusCase = false;
        $errorMsgs = array();
        if (Yii::app()->request->IsPostRequest) {
            $result = FSM::run('User.user.login', $_POST);
            $user = $result->model;
            if ($this->post('ajax','') == 'user-login-form'){
                echo $result->getActiveErrorMessages($user);
                Yii::app()->end();
            }
            if (! $result->hasErrors())
            {
                if (($url = Yii::app()->user->returnUrl) != '')
                    Yii::app()->controller->redirect($url);
                else
                    Yii::app()->controller->redirect(array('/Admin'));
            }
            else
            {
                $errors = $result->getErrors();
                if(isset($errors['ErrorCode']) && is_array($errors['ErrorCode']))
                    $errorMsgs = $errors['ErrorCode'];
            }
        }
        if (Yii::app()->user->isGuest === false) {
            $this->redirect(Yii::app()->request->getBaseUrl(true));
        }
        $this->layout = 'login';
        $this->render('login', array('user' => $user,'errorMsgs'=>$errorMsgs));
    }
    
    public function actionActivateAccount()
    {
        Yii::import('User.models.User');
        $user_id = $this->get('id',0);
        $code = $this->get('code','');
        
        $user = User::model()->find('id=:id AND validation_type=:type AND validation_code=:code AND status=:status',array(':id'=>$user_id,':type'=>User::REGISTER_VALIDATION_TYPE,':code'=>$code,':status'=>User::STATUS_DEACTIVE));
        if (!is_null($user))
        {
            $user->status = User::STATUS_ACTIVE;
            $user->validation_type = 0;
            $user->validation_code = '';
            $user->update(array('status','validation_type','validation_code'));    
        }
        
        $this->layout = 'login';
        $this->render('activate_account', array('user'=>$user));
    }
    
    public function actionForgotPassword()
    {
        Yii::import('User.models.User');
        $user = new User('forgot_password');
        $errorMsgs = array();
        $request_password_suc = false;
        if (Yii::app()->request->IsPostRequest && isset($_POST['User'])) {
            $user->setAttributes($_POST['User'],false);
            $result = FSM::run('User.AdminUser.forgotPassword', $_POST);
            if (! $result->hasErrors())
            {
                $request_password_suc = true;
            }
            else
            {
                $errors = $result->getErrors();
                if(isset($errors['ErrorCode']) && is_array($errors['ErrorCode']))
                    $errorMsgs = $errors['ErrorCode'];
            }
        }
        $this->layout = 'login';
        $this->render('forgot_password', array('user' => $user,'errorMsgs'=>$errorMsgs,'request_password_suc'=>$request_password_suc));
    }
    
    public function actionConfirmResetPassword()
    {
        Yii::import('User.models.User');
        $user_id = $this->get('id',0);
        $code = $this->get('code','');
        
        $user = User::model()->find('id=:id AND validation_type=:type AND validation_code=:code AND status=:status',array(':id'=>$user_id,':type'=>User::FORGOT_PASSWORD_VALIDATION_TYPE,':code'=>$code,':status'=>User::STATUS_ACTIVE));
        if (!is_null($user))
        {
            $result = FSM::run('User.AdminUser.resetPassword', array('user'=>$user));    
        }
        
        $this->layout = 'login';
        $this->render('confirm_reset_password', array('user'=>$user));    
    }
}